//
//  DrawRectView.swift
//  Dynamic
//
//  Created by Yinyin Zeng on 26/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit
//重力和碰撞
class DrawRectView: UIView, UICollisionBehaviorDelegate {
    
    
    
    let squareViewOne = UIImageView()
    let squareViewTwo = UIImageView()
    var animator = UIDynamicAnimator()
    let squareViewBezierPath = UIBezierPath()
    let arrowLine = UIImageView(image:  UIImage(named: "Arrow"))
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: 0, y: 200))
        bezierPath.addLine(to: CGPoint(x: 200, y: 250))
        bezierPath .stroke()
        
        //查看方块的真实frame
//        let rectBezierPath = UIBezierPath(rect: squareViewOne.frame)
//        rectBezierPath.stroke()
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        self.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundTile")!)
        
        squareViewOne.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        squareViewOne.image = UIImage(named: "Box1")
        self.addSubview(squareViewOne)
        
        squareViewTwo.frame = CGRect(x: 170, y: UIScreen.main.bounds.size.height - 100, width: 80, height: 80)
        squareViewTwo.image = UIImage(named: "Box2")
        self.addSubview(squareViewTwo)
        
        //底部分界线
        arrowLine.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 20, width: UIScreen.main.bounds.size.width, height: 20)
        arrowLine.backgroundColor = .gray
        self.addSubview(arrowLine)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重力和碰撞
    @objc func gravityAction(){
        //根据一个视图 创建动画者对象
        animator = UIDynamicAnimator(referenceView: self)
        //根据某一个动力学元素 创建行为
        //重力
        let gravity = UIGravityBehavior(items: [self.squareViewOne])
        //重力的方向
        //        gravity.angle = CGFloat(Double.pi) //用角度的形式设置方向
        //        gravity.gravityDirection = CGVector(dx: 1, dy: 1) //以向量的形式设置方向
        //重力的量级
        //        gravity.magnitude = 10
        
        //碰撞
        let collisition = UICollisionBehavior(items: [self.squareViewOne,self.squareViewTwo])
        //把引用view的bounds变成边界
        collisition.translatesReferenceBoundsIntoBoundary = true
        
        //添加边界
        //以一条线为边界
        collisition.addBoundary(withIdentifier: "key" as NSCopying, from: CGPoint(x: 0, y: 200), to: CGPoint(x: 200, y: 250))
        
        //底部边界
        let path = UIBezierPath.init(rect: self.arrowLine.frame)
        collisition.addBoundary(withIdentifier: "keyTwo" as NSCopying, for: path)
        
        //以一个自定义的路径为边界 在squareViewTwo外面套了一层边界
        //        let path = UIBezierPath.init(rect: self.squareViewTwo.frame)
        //        collisition.addBoundary(withIdentifier: "keyTwo" as NSCopying, for: path)
        
        //action 实时监听frame的变化
        collisition.action = {
//            self.setNeedsDisplay()
            
            if self.squareViewOne.frame.width > 105{
                self.squareViewOne.backgroundColor = .lightGray
            }else{
                self.squareViewOne.backgroundColor = .clear
            }
        }
        
        collisition.collisionDelegate = self
        
        //碰撞模式
        //        items         只与视图Item碰撞
        //        boundaries    只与边界碰撞
        //        everything    默认模式-所有
        //        collisition.collisionMode = everything
        
        //把行为添加到动画中
        animator.addBehavior(gravity)
        animator.addBehavior(collisition)
    }
    
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let currentPoint = touch.location(in: touch.view)
            if squareViewOne.frame.contains(currentPoint){
                self.squareViewOne.center = currentPoint
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.gravityAction()
    }
    
    //MARK: - 碰撞代理方法

    //两物体开始碰撞执行的方法
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item1: UIDynamicItem, with item2: UIDynamicItem, at p: CGPoint) {
        
    }
    //和某个命名ID的线开始碰撞执行。 Identifier为nil 默认是屏幕四周
    private func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?) {
        
    }

    
}
